//#include <a_samp>
#include <a_players>
#include <string>
#tryinclude num_players

#include <myfuncs>
#include <myfuncsdll>


new gActivePlayers;

static currvote;
static voted[NUM_PLAYERS]; // 0 - not voted, 1 - no, 2 - yes

forward VoteTimeout();

public OnFilterScriptInit()
{
	currvote = -1;
	gActivePlayers = 0;
	for(new i=0; i<NUM_PLAYERS; i++)
		if(IsPlayerConnected(i)) gActivePlayers++;
	return true;
}

public OnPlayerConnect(playerid)
{
	gActivePlayers++;
	return true;
}

public OnPlayerDisconnect(playerid, reason)
{
	if(gActivePlayers > 0) gActivePlayers--;
	if(playerid == currvote)
		currvote = -1;
	else
		voted[playerid] = 0;
	return true;
}


public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[96], idx=0;
	mystrtok(cmd, cmdtext, idx);
	if(strcmd(cmd, "/votekick"))
	{
		new msg[96];
		if(IsPlayerConnected(currvote)) return SendClientMessage(playerid, COLOR_ERROR, "Another vote is currently in progress");
		if(gActivePlayers <= 2) return SendClientMessage(playerid, COLOR_ERROR,
			"There are not enough players in the game to start a vote");
		mystrtok(msg, cmdtext, idx);
		if(isnull(msg)) return SendClientMessage(playerid, COLOR_USAGE, "USAGE: /votekick [playerid]");
		else currvote = strid(msg);
		if(!IsPlayerConnected(currvote))
		{
			SendClientMessage(playerid, COLOR_ERROR, "You cannot kick a player that is not yet in the game");
			currvote = -1;
			return true;
		}
		SendClientMessage(playerid, COLOR_INFO3, "Kick vote initiated");
		format(msg, sizeof(msg), "%s has initiated a Kick vote against %s. This vote will end in 4 minutes. Use /+ or /- to vote",
			GetFormattedName(playerid), GetFormattedName(currvote));
		SendClientMessageToAll(COLOR_INFO2, msg);
		SetTimer("VoteTimeout", 4 * 60000, false);
		return true;
	}
	if(strcmd(cmd, "/+")||strcmd(cmd, "/-"))
	{
		new msg[96];
		if(currvote == playerid) return SendClientMessage(playerid, COLOR_ERROR, "You cannot vote against yourself");
		if(voted[playerid] != 0) return SendClientMessage(playerid, COLOR_ERROR,
			"You have already voted for this player");
		else voted[playerid] = (cmd[1] == '+')?2:1;
		format(msg, sizeof(msg), "Voted %s", (cmd[1] == '+')?("Yes"):("No"));
		SendClientMessage(playerid, COLOR_INFO3, msg);
/*		if(VoteCount())
		{
			format(msg, sizeof(msg), "%s vote(s) for kicking %s, %d vote(s) needed. Player %s kicked.",
				GetVoteCount(), GetFormattedName(currvote), (gActivePlayers/2)+1, GetFormattedName(currvote));
			SendClientMessageToAll(COLOR_INFO2, msg);
			Kick(currvote);
			VoteEnd();
		} else {*/
		if(!VoteCount())
		{
			format(msg, sizeof(msg), "%d vote(s) for kicking %s, %d vote(s) are needed. Use /+ or /- to vote.",
				GetVoteCount(), GetFormattedName(currvote), (gActivePlayers/2)+1);
			SendClientMessageToAll(COLOR_INFO2, msg);
		}
		/*format(msg, sizeof(msg), "Player %s has voted. Use /+ or /- to vote for kick player %s.",
			GetFormattedName(playerid), GetFormattedName(currvote));*/
		return true;
	}

	return false;
}

stock VoteCount()
{
	if(GetVoteCount()>=((gActivePlayers/2)+1))
	{
		new msg[96];
		format(msg, sizeof(msg), "%d vote(s) for kicking %s, %d vote(s) needed. Player %s kicked.",
			GetVoteCount(), GetFormattedName(currvote), (gActivePlayers/2)+1, GetFormattedName(currvote));
		SendClientMessageToAll(COLOR_INFO2, msg);
		Kick(currvote);
		VoteEnd();
		return true;
	}
	return false;
}

stock GetVoteCount()
{
	new i, count;
	for(count=0, i=0; i<NUM_PLAYERS; i++)
		if(IsPlayerConnected(i)&&voted[i]==2) count++;
	return(count);
}


stock VoteEnd()
{
	for(new i=0; i<NUM_PLAYERS; i++)
		voted[i] = 0;
	currvote = -1;
	return true;
}

public VoteTimeout()
{
	if(!IsPlayerConnected(currvote)) return false;
	if(GetVoteCount()<((gActivePlayers/2)+1))
	{
		new msg[96];
		format(msg, sizeof(msg), "%d vote(s) for kicking %s, %d vote(s) needed. Player %s stays.",
			GetVoteCount(), GetFormattedName(currvote), (gActivePlayers/2)+1,GetFormattedName(currvote));
		SendClientMessageToAll(COLOR_INFO2, msg);
		VoteEnd();
	} else return VoteCount();
	return true;
}

